package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.dto.bedsideScreen.BindBedsideScreenDto;
import com.csun.cmny.provider.model.dto.bedsideScreen.BindCMBedsideScreenDto;
import com.csun.cmny.provider.model.dto.device.DevicePageQuery;
import com.csun.cmny.provider.model.dto.device.DmcEditDeviceDto;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcBedsideScreenBindService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-08 11:57
 * @Description: the class dmc device follow controller.
 */
@RestController
@RequestMapping(value = "/bedsideScreen", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcBedsideScreenMainController extends BaseController {

    @Resource
    private DmcDeviceService dmcDeviceService;

    @Resource
    private DmcBedsideScreenBindService dmcBedsideScreenBindService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 添加床头屏设备
     *
     * @param dmcEditDeviceDto  the dmc edit device dto
     * @param br                the binding result
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "添加床头屏设备")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addBedsideScreen(@Validated @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();

        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.BEDSIDE_SCREEN.getIndex());

        Integer result = dmcDeviceService.addDevice(dmcDevice, getLoginAuthDto());

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }


    /**
     * 绑定床头屏和床位
     *
     * @param bindBedsideScreenDto  the dmc edit device dto
     * @param
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "绑定床头屏和床位")
    @RequestMapping(value = "/bind",method = RequestMethod.POST)
    public Wrapper bindBedsideScreen(@Validated  @RequestBody BindBedsideScreenDto bindBedsideScreenDto) {

        Integer result = dmcBedsideScreenBindService.bind(bindBedsideScreenDto, getLoginAuthDto());

        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 解绑床头屏和床位
     *
     * @param  deviceId
     *
     * @return  the wrapper
     */
    //@LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/break/{deviceId}", method = RequestMethod.POST)
    public Wrapper breakBedsideCare(@PathVariable Long deviceId) {


        Integer result = dmcBedsideScreenBindService.breakSleepCare(deviceId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 修改床头屏设备
     *
     * @param dmcEditDeviceDto  the dmc edit device dto
     * @param br                the binding result
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改床头屏设备")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper editBedside(@Validated @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();
        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.BEDSIDE_SCREEN.getIndex());

        Integer result = dmcDeviceService.editDevice(dmcDevice, getLoginAuthDto());

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 根据Id删除床头屏
     *
     * @param id    the id
     *
     * @return  the wrapper
     */
    @ApiOperation(httpMethod = "POST", value = "根据Id删除床头屏")
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public Wrapper deleteDmcBedsideById(@PathVariable Long id) {

        DmcDevice dmcDeviceOld = dmcDeviceService.selectByKey(id);

        Integer result = dmcBedsideScreenBindService.deleteCsunBedsideById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        } else {
            redisTemplate.delete(RedisKeyUtil.getCsunBedsideKey(dmcDeviceOld.getDeviceNo()));
        }

        //return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }


    /**
     * 查询床头屏设备列表
     *
     * @param devicePageQuery
     * the sleep care page query
     *
     * @return  the wrapper
     */
    @ApiOperation(httpMethod = "POST", value = "查询床头屏设备列表")
    @RequestMapping(value = "/queryGroupBedsideCareListWithPage", method = RequestMethod.POST)
    public Wrapper queryGroupBedsideCareListWithPage(@RequestBody DevicePageQuery devicePageQuery) {

        devicePageQuery.setGroupId(getLoginAuthDto().getGroupId());
        devicePageQuery.setDeviceType(DmcDeviceTypeEnum.BEDSIDE_SCREEN.getIndex());

        PageInfo pageInfo = dmcDeviceService.queryBedsideListWithPage(devicePageQuery);

        //return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * 查询床头屏设备列表-崇明
     *
     * @param devicePageQuery
     * the sleep care page query
     *
     * @return  the wrapper
     */
    @ApiOperation(httpMethod = "POST", value = "查询床头屏设备列表")
    @RequestMapping(value = "/queryGroupCMBedsideCareListWithPage", method = RequestMethod.POST)
    public Wrapper queryGroupCMBedsideCareListWithPage(@RequestBody DevicePageQuery devicePageQuery) {

        devicePageQuery.setGroupId(getLoginAuthDto().getGroupId());
        devicePageQuery.setDeviceType(DmcDeviceTypeEnum.BEDSIDE_SCREEN.getIndex());

        PageInfo pageInfo = dmcDeviceService.queryCMBedsideListWithPage(devicePageQuery);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * 绑定床头屏和房间号
     *
     * @param bindCMBedsideScreenDto  the dmc edit device dto
     * @param
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "绑定床头屏和床位")
    @RequestMapping(value = "/bindWithRoom",method = RequestMethod.POST)
    public Wrapper bindBedsideScreenWithRoom(@Validated @RequestBody BindCMBedsideScreenDto bindCMBedsideScreenDto, BindingResult br) {

        Integer result = dmcBedsideScreenBindService.bindWithRoom(bindCMBedsideScreenDto, getLoginAuthDto());

        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 解绑床头屏和房间号
     *
     * @param  deviceId
     *
     * @return  the wrapper
     */
    //@LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/breakWithRoom/{deviceId}", method = RequestMethod.POST)
    public Wrapper breakBedsideCareWithRoom(@PathVariable Long deviceId) {


        Integer result = dmcBedsideScreenBindService.breakSleepCareWithRomm(deviceId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }


}
